package voldemort.store.cachestore;

/**
 * Created by IntelliJ IDEA.
 * User: mhsieh
 * Date: 2/4/11
 * Time: 10:12 AM
 * To change this template use File | Settings | File Templates.
 */
public class CLIUtils {

    public static int match(String arg, String[] opts) {
        for ( int i = 0; i < opts.length ; i ++) {
            if ( arg.equals(opts[i]) )  return i;
        }
        return -1;
    }

    public static void help(String[] opts, String[] defaults ) {
        StringBuffer sb = new StringBuffer();
        for ( int i = 0; i < opts.length ; i ++ )
        {
            sb.append( ( i < opts.length ? opts[i] :" " ) + " " + ( i < defaults.length ? defaults [i]: " ") );
        }
        System.out.println("java TestClient "+ sb.toString() );
    }

//    public static int inActions(String action, String[] opts){
//        for ( int i = 0; i < opts.length ; i ++) {
//            if ( (opts[i]).indexOf(action+",") >=0 )  return i;
//        }
//        return -1;
//    }

    public static String[] getOpts(String[] args, String[] opts, String[] defaults) {
        if ( args.length == 0 || opts.length == 0  ) return defaults;
        for (int i =0 ; i < args.length ; ) {
            if ( args[i].equals("-help") || args[i].equals("-h"))  {
                help(opts, defaults );
                System.exit(1 );
            }
            else {
                int j = match( args[i], opts);
                if ( j >= 0 && j < defaults.length ) {
                     defaults[j] = args[ i+1 ];
                     i += 2;
                }
                else {
                    System.out.println("opts "+args[i]+" is not support");
                    help(opts, defaults );
                    System.exit(1 );
                }
            }
        }
        return defaults;
    }


}
